package com.xiaobao.coalproperty.module.user.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class DealPasswordActivity extends BaseActivity {

    @BindView(R.id.deal_pwd)
    EditText edtPwd;
    @BindView(R.id.deal_pwd_again)
    EditText edtPwdAgain;

    @Override
    protected int getContentView() {
        return R.layout.activity_deal_passwrod;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("交易密码");
        edtPwd.setCursorVisible(false);
        edtPwdAgain.setCursorVisible(false);
    }

    public void submitClick(View view) {
        if (TextUtils.isEmpty(edtPwd.getText().toString())) {
            showToast("密码不能为空");
            return;
        }
        if (!edtPwd.getText().toString().equals(edtPwdAgain.getText().toString())) {
            showToast("两次密码不一致");
            return;
        }
        if (edtPwd.getText().toString().length() < 6 || edtPwd.getText().toString().length() > 12) {
            showToast("密码长度在6-12之间");
            return;
        }
        if (isSpecial(edtPwd.getText().toString())) {
            showToast("密码不允许包含特殊字符");
            return;
        }
        setTransactionPassword();
    }

    private void setTransactionPassword() {
        OkHttpUtils.post().url(UrlConstant.URL_SET_TRANSACTION_PASSWORD)
                .params(HttpHelperUtils.getInstance().setTransactionPasswordParams(edtPwd.getText().toString(), edtPwdAgain.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    public boolean isSpecial(String mobileNums) {

        String telRegex = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }
}
